package com.lanzchina.lanclass.utils;

import com.lanzchina.lanclass.constant.Constant;
import sun.security.provider.MD5;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by qiangjindong
 * 2018-08-10 17:51
 */
public class Md5Util {
    public static String md5(String string) {
        if (string.isEmpty()) {
            return "";
        }
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String validateCode(String key, String value) {
        return md5(key + "=" + value + "&" + Constant.SIGN_KEY);
    }

    public static void main(String[] args) {
        System.out.println(Md5Util.validateCode("code", "a"));
        System.out.println(md5(Constant.SIGN_KEY));
        System.out.println(validateCode("schoolCode", "12222"));
        System.out.println(validateCode("userAccount", "1808110001"));
        System.out.println(md5("123456"));
    }
}
